package com.caigou.modules.data.repository

import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.bean.HttpResult
import com.caigou.modules.data.bean.*
import com.caigou.modules.data.network.HttpDataService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/26
 */
@ActivityRetainedScoped
class DataRepository @Inject constructor() {

    fun getDataTypeList(): Flow<HttpResult<MutableList<DataTypeItemBean>>> {
        return HttpDataService.service.getDataTypeList()
    }

    fun getDataList(categoryId: Int): Flow<HttpResult<MutableList<DataItemBean>>> {
        return HttpDataService.service.getDataList(categoryId)
    }

    fun getLawList(): Flow<HttpResult<MutableList<LawItemBean>>> {
        return HttpDataService.service.getLawList()
    }

    fun getAllYearList(): Flow<HttpResult<MutableList<AllYearItemBean>>> {
        return HttpDataService.service.getAllYearList()
    }

    fun getDataDetail(categoryId: Int, mysteryId: Int, userId: String): Flow<HttpResult<DataDetailBean>> {
        return HttpDataService.service.getDataDetail(categoryId, mysteryId, userId)
    }

    fun getLawDetail(formulaId: Int, userId: String): Flow<HttpResult<LawDetailBean>> {
        return HttpDataService.service.getLawDetail(formulaId, userId)
    }

    fun getAllYearDetail(mysteryId: Int, userId: String): Flow<HttpResult<ALlYearDetailBean>> {
        return HttpDataService.service.getAllYearDetail(mysteryId, userId)
    }

    fun getCommentList(page: Int, limit: Int, dataId: Int, type: Int, sort: Int, currentUserId: String): Flow<HttpResult<CommentBean>> {
        return HttpDataService.service.getCommentList(page, limit, dataId, type, sort, currentUserId)
    }

    fun getCommentDetail(dataId: Int, replyId: Int, userId: String, type: Int): Flow<HttpResult<CommentItemBean>> {
        return HttpDataService.service.getCommentDetail(dataId, replyId, userId, type)
    }

    fun setReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int, type: Int): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", dataId.toString())
            .addFormDataPart("type", type.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpDataService.service.setReply(body.build())
    }

    fun setCommentReply(
        content: String, files: MutableList<File>,
        senderId: String, receiverId: String,
        dataId: Int, type: Int, parentId: Int
    ): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", dataId.toString())
            .addFormDataPart("type", type.toString())
            .addFormDataPart("parentId", parentId.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpDataService.service.setReply(body.build())
    }

    fun setDataLike(dataId: Int, type: Int, senderId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("dataId", dataId)
        jsonObject.put("type", type)
        jsonObject.put("senderId", senderId)
        val body = jsonObject.toString().toRequestBody()
        return HttpDataService.service.setDataLike(body)
    }

    fun setCommentLike(replyId: Int, type: Int, senderId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("replyId", replyId)
        jsonObject.put("type", type)
        jsonObject.put("senderId", senderId)
        jsonObject.put("receiverId", receiverId)
        val body = jsonObject.toString().toRequestBody()
        return HttpDataService.service.setCommentLike(body)
    }
}